﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类TableDownload。
    /// </summary>
    public class TableDownload
    {
        public TableDownload()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_TableDownload where id=" + id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.TableDownload model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_TableDownload(");
            strSql.Append("tname,yu_name,bm_name,info,link,types,addtime,downs,zt,fbren");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.tname + "',");
            strSql.Append("'" + model.yu_name + "',");
            strSql.Append("'" + model.bm_name + "',");
            strSql.Append("'" + model.info + "',");
            strSql.Append("'" + model.link + "',");
            strSql.Append("'" + model.types + "',");
            strSql.Append("'" + model.addtime + "',");
            strSql.Append("" + model.downs + ",");
            strSql.Append("" + model.zt + ",");
            strSql.Append("'" + model.fbren + "'");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.TableDownload model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_TableDownload set ");
            if (model.tname != null)
                strSql.Append("tname='" + model.tname + "',");
            if (model.yu_name != null)
                strSql.Append("yu_name='" + model.yu_name + "',");
            if (model.bm_name != null)
                strSql.Append("bm_name='" + model.bm_name + "',");
            if (model.info != null)
                strSql.Append("info='" + model.info + "',");
            if (model.link != null)
                strSql.Append("link='" + model.link + "',");
            if (model.types != null)
                strSql.Append("types='" + model.types + "',");
            if (model.downs.Equals(Int32.Parse("0")) != true)
                strSql.Append("downs=" + model.downs + ",");
            if (model.zt.Equals(Int32.Parse("0")) != true)
                strSql.Append("zt=" + model.zt + ",");
            if (model.fbren != null)
                strSql.Append("fbren='" + model.fbren + "',");
            strSql.Append("addtime=addtime ");
            strSql.Append(" where id=" + model.id + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_TableDownload ");
            strSql.Append(" where id=" + id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        public bool Delete(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_TableDownload ");
            strSql.Append(" where id in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 审核消审操作
        /// </summary>
        /// <param name="ids">ID</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public bool UpdateZT(string ids, string zt)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_TableDownload set ");
            strSql.Append(" zt = " + zt + "");
            strSql.Append(" where id in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.TableDownload GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [id],[tname],[yu_name],[bm_name],[info],[link],[types],[addtime],[downs],[zt],[fbren] ");
            strSql.Append(" from lt_TableDownload ");
            strSql.Append(" where id=" + id);
            ltModel.Model.TableDownload model = new ltModel.Model.TableDownload();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.id = id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.tname = ds.Tables[0].Rows[0]["tname"].ToString();
                model.yu_name = ds.Tables[0].Rows[0]["yu_name"].ToString();
                model.bm_name = ds.Tables[0].Rows[0]["bm_name"].ToString();
                model.info = ds.Tables[0].Rows[0]["info"].ToString();
                model.link = ds.Tables[0].Rows[0]["link"].ToString();
                model.types = ds.Tables[0].Rows[0]["types"].ToString();
                model.addtime = ds.Tables[0].Rows[0]["addtime"].ToString();
                if (ds.Tables[0].Rows[0]["downs"].ToString() != "")
                {
                    model.downs = int.Parse(ds.Tables[0].Rows[0]["downs"].ToString());
                }
                if (ds.Tables[0].Rows[0]["zt"].ToString() != "")
                {
                    model.zt = int.Parse(ds.Tables[0].Rows[0]["zt"].ToString());
                }
                model.fbren = ds.Tables[0].Rows[0]["fbren"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id],[tname],[yu_name],[bm_name],[info],[link],[types],[addtime],[downs],[zt],[fbren] ");
            strSql.Append(" FROM lt_TableDownload ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string topNum, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (PageValidate.IsNumber(topNum) && topNum != "0")
                strSql.Append("top " + topNum + " ");
            strSql.Append(" [id],[tname],[yu_name],[bm_name],[info],[link],[types],[addtime],[downs],[zt],[fbren] ");
            strSql.Append(" FROM lt_TableDownload ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        #endregion  成员方法
    }
}